package ds.students;

import ds.interfaces.List;

/**
 * @author simont
 */
public class DSList implements List {

    public Node head;

    public DSList() {
        head = null;
    }

    public DSList(Node head_) {
		Node self = new Node(null,null,head_.getToken());
		head = head_;
		Node temp = head_;
		while(temp!=null && temp.next!=null){
			Node self2 = new Node(null,self,temp.next.getToken());
			self.next = self2;
			self = self2;
			temp = temp.next;
		}
    }

    public DSList(DSList other) { // Copy constructor.
        Node self = new Node(null,null,other.head.getToken());
        head = self;
        Node temp = other.head;
        while(temp!=null && temp.next!=null){
        	Node self2 = new Node(null,self,temp.next.getToken());
        	self.next = self2;
        	self = self2;
        	temp = temp.next;
		}
    }

    @Override
    public Token remove(int index){
    	if (index ==0) {
    		Node temp = head;
			head = head.next;
			return temp.getToken();
		}
        Node temp = head.next;
        int cur_index = 1;
        while (temp != null) {
            if (cur_index == index) {
            	temp.prev.next = temp.next;
				return temp.getToken();
			}
            else {
				temp = temp.next;
				cur_index++;
			}
        }
        throw new IndexOutOfBoundsException();
    }

    @Override
    public int indexOf(Token obj) {
    	int cur_index = 0;
    	Node temp = head;
    	while (temp!=null){
    		if (temp.getToken().equals(obj))
    			return cur_index;
    		else {
				temp = temp.next;
				cur_index++;
			}
		}
        return -1;
    }

    @Override
    public Token get(int index) {
		Node temp = head;
		int cur_index = 0;
		while (temp != null) {
			if (cur_index == index) {
				return temp.getToken();
			}
			else {
				temp = temp.next;
				cur_index++;
			}
		}
		return null;
    }

    @Override
    public boolean isEmpty() {
    	if (head ==null)
        	return true;
    	else
    		return false;
    }

    @Override
    public int size() {
		int cur_index = 0;
		Node temp = head;
		while (temp!=null){
				temp = temp.next;
				cur_index++;
		}
		return cur_index;
    }

    @Override
    public String toString() {
    	String result = "";
		Node temp = head;
		while (temp!=null){
			result += temp.getToken().toString();
			if (temp.next!=null)
				result+=" ";
			temp = temp.next;
		}
        return result;
    }

    @Override
    public boolean add(Token obj) {
		int cur_index = 0;
		Node temp = head;
		if (temp ==null){
			head = new Node(null,null,obj);
			return true;
		}

		while (temp.next!=null){
			temp = temp.next;
			cur_index++;
		}
		temp.next = new Node(null,temp,obj);
        return true;
    }

    @Override
    public boolean add(int index, Token obj) {
		int cur_index = 1;
		if (index == 0)
		{
			Node temp = head;
			head = new Node(temp,null,obj);
			return true;
		}
		Node temp = head;
		while (temp!=null){
			if (index == cur_index)
			{
				Node rem = temp.next;
				temp.next = new Node(rem,temp,obj);
				return true;
			}
			temp = temp.next;
			cur_index++;
		}
		return false;
    }

    @Override
    public boolean contains(Token obj) {
    	if (indexOf(obj)!=-1)
    		return true;
        return false;
    }

    @Override
    public boolean remove(Token obj) {
    	remove(indexOf(obj));
    	return true;
    }

    @Override
    public int hashCode() {
        return 0;
    }

    @Override
    public boolean equals(Object other) {
    	if (toString().equals(other.toString()))
        	return true;
    	else
    		return false;
    }

}
